package com.framework.app;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;

import com.framework.diarylibrary.DiaryItemEntity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainViewModel extends AndroidViewModel {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public ObservableField<List<DiaryItemEntity>> events = new ObservableField<>();
    public MainViewModel(@NonNull Application application) {
        super(application);
    }


    /**
     * msgId 日程id
     * time1
     * time2 起止时间  2020-10-24 10:24
     */
    private DiaryItemEntity setDiaryEntity(String id, String title, String location, int color, String time1, String time2, boolean allDay) {

        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();

        try {
            startTime.setTime(sdf.parse(time1));
            endTime.setTime(sdf.parse(time2));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        DiaryItemEntity diaryItemEntity = new DiaryItemEntity(id, title, location, startTime, endTime, allDay);
        diaryItemEntity.setColor(ContextCompat.getColor(getApplication(), color));
        return diaryItemEntity;
    }


    void addTestData() {
        List<DiaryItemEntity> data = new ArrayList<>(6);
        data.add(setDiaryEntity("123", "测试1", "厕所", R.color.event_color_01, "2020-07-01 10:24:33", "2020-07-01 12:24:33", false));
        data.add(setDiaryEntity("789", "测试2", "厕所", R.color.event_color_03, "2020-07-01 9:24:33", "2020-07-01 15:24:33", false));
        data.add(setDiaryEntity("789", "测试2", "厕所", R.color.event_color_03, "2020-07-03 19:24:33", "2020-07-03 20:24:33", false));
        data.add(setDiaryEntity("789", "测试2", "厕所", R.color.event_color_03, "2020-07-03 11:24:33", "2020-07-03 16:24:33", false));
        data.add(setDiaryEntity("789", "测试2", "厕所", R.color.event_color_03, "2020-07-03 9:24:33", "2020-07-03 18:24:33", false));
        data.add(setDiaryEntity("456", "测试2", "食堂", R.color.event_color_02, "2020-07-03 12:24:33", "2020-07-04 12:24:33", true));
        events.set(data);

    }
}